﻿#include "scau.h"
#include <QEventLoop>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtWidgets/QWidget>
#include <QTextCodec>
#include <QString>
#include <QUrl>
#include <QJsonParseError>
#include <QJsonObject>
#include <QRegExp>
#include <QRegularExpression>


scau::scau(database* Tempdb)
{
    db=Tempdb;
}



//华南农业大学
//寻找下一页
QString scau::search_next_page(QString data)
{

    QString i;

    QStringList temp1;

    QStringList temp;

    i = QString("(.*)下一页(.*)");

    temp1 = search(i, data);

    if (temp1.size() > 0)
    {
        temp = search("(.*)href=\"", temp1[0]);

        temp1[0] = temp1[0].replace(temp[0], "");

        temp = search("\" target(.*)", temp1[0]);

        temp1[0] = temp1[0].replace(temp[0], "");

        //qDebug()<<temp1[0];

        return temp1[0];

    }

    else

    {
        qDebug() << "没有找到上下页";

        return "";

    }

}

//寻找尾页
QString scau::search_final_page(QString data)
{

    QString i;

    QStringList temp1;

    QStringList temp;

    i = QString("(.*)尾页(.*)");

    temp1 = search(i, data);

    if (temp1.size() > 0)
    {
        temp = search("(.*)href=\"", temp1[0]);

        temp1[0] = temp1[0].replace(temp[0], "");

        temp = search("\" target(.*)", temp1[0]);

        temp1[0] = temp1[0].replace(temp[0], "");

        //qDebug()<<temp1[0];

        return temp1[0];

    }

    else

    {
        qDebug() << "没有找到尾页";

        return "";

    }

}

//找到讲座的网址
QStringList scau::search_url(QString data)
{

    QString i = QString("<a href=(.*)title='学术报告|<a href=(.*)title='第三届");

    QStringList temp;

    temp = search(i, data);


    //移除非网址部分

    QString i1 = QString("(.*)href='");

    QString i2 = QString("' target(.*)");

    QStringList useless_part;

    for (int a = 0; a < temp.length(); a++)

    {

        //qDebug()<<linshi[a];

        useless_part = search(i1, temp[a]);

        temp[a].replace(useless_part[0], "");

        useless_part = search(i2, temp[a]);

        temp[a].replace(useless_part[0], "");

    }

    return temp;

}


//抓取发布时间
QStringList scau::search_date(QString data)
{
    QString i = "发布时间:(.*?)</span>";

    QStringList temp;
    temp=search(i, data);

    QStringList post_date;

    if (temp.size() > 0)
    {
        //删去无效信息
        for (int a = 0; a < temp.size(); a++)
        {
            temp[a].replace("发布时间:", "");
            temp[a].replace("</span>", "");

            post_date.append(temp[a]);
        }

    }
    return post_date;

}


QStringList scau::search_data(QString data)
{

    //抓取论文信息

    QStringList title, lecturer, time, place, university, url;

    QStringList useless_part;

    useless_part << "&nbsp;" << "<(.*?)>"<<" ";

    QStringList temp;



    //查找标题
    QString regulardata = "学术报告：(.*?)</h2>";
    QString titledata_1 = "报告主题(.*?)</strong>";
    QString titledata_2 = "报告题目(.*?)<";
    QString titledata_3 = "题目(.*?)<";
    QString titledata_4 = "学术报告(.*?)：(.*?)<";

    QStringList searchdata = search(regulardata, data);
    QStringList searchdata_1 = search(titledata_1, data);
    QStringList searchdata_2 = search(titledata_2, data);
    QStringList searchdata_3 = search(titledata_3, data);
    QStringList searchdata_4 = search(titledata_4, data);

    if (searchdata.size() > 0)
    {
        //删去无效信息
        for (int a = 0; a < searchdata.size(); a++)
        {
            searchdata[a].replace("</h2>", "");
            searchdata[a].replace("学术报告：", "");
            title.append(searchdata[a]);
        }
    }
    else if(searchdata_1.size()>0)
    {
        for (int a = 0; a < searchdata_1.size(); a++)
        {
            QString temp_1 = "报告主题(.*?)：";
            QStringList temp_2 = search(temp_1,searchdata_1[a]);

            searchdata_1[a].replace("</strong>", "");
            searchdata_1[a].replace("</span>", "");

            for(int i = 0; i < temp_2.length(); i++)
            {
                searchdata_1[a].replace(temp_2[i], "");
            }

            title.append(searchdata_1[a]);
        }
    }
    else if(searchdata_2.size()>0)
    {
        for (int a = 0; a < searchdata_2.size(); a++)
        {
            QString temp_1 = "报告题目(.*?)：";
            QStringList temp_2 = search(temp_1,searchdata_2[a]);

            searchdata_2[a].replace("<", "");
            for(int i = 0; i < temp_2.length(); i++)
            {
                searchdata_2[a].replace(temp_2[i], "");
            }

            title.append(searchdata_2[a]);
        }
    }
    else if(searchdata_3.size()>0)
    {
        for (int a = 0; a < searchdata_3.size(); a++)
        {
            searchdata_3[a].replace("题目：", "");
            searchdata_3[a].replace("<", "");

            title.append(searchdata_3[a]);
        }
    }
    else if(searchdata_4.size()>0)
    {
        for (int a = 0; a < searchdata_4.size(); a++)
        {
            QString temp_1 = "学术报告(.*?)：";
            QStringList temp_2 = search(temp_1,searchdata_4[a]);

            searchdata_4[a].replace("<", "");
            for(int i = 0; i < temp_2.length(); i++)
            {
                searchdata_4[a].replace(temp_2[i], "");
            }

            title.append(searchdata_4[a]);
        }
    }
    else
    {
        qDebug() << "查找标题时发生错误";

        title.append("出错\r");
    }

    //qDebug()<<title;


    //处理详细内容信息

    regulardata = "<div class='wp_articlecontent'>(.*)";

    data = search(regulardata, data)[0];

    if (data.size() > 0)
    {
        for (int b = 0; b < useless_part.size(); b++)
        {
            temp = search(useless_part[b], data);
            if (temp.size() > 0)
                for (int c = 0; c < temp.size(); c++)
                    data.replace(temp[c], "");
        }
    }

    data = data.replace(QRegExp("时间：2019年9月21日上午08:30-11:40下午14:00-18:00地点："), "");//特殊处理


    //获取报告人
    regulardata = "报告人：(.*?)时间";
    QString lecturerdata_1 = "报告人:(.*?)时间";//特殊处理
    QString lecturerdata_2 = "报告人：(.*?)日期";
    QString lecturerdata_3 = "报告人：(.*?)题目";
    QString lecturerdata_4 = "主讲人：(.*?)时间";

    searchdata = search(regulardata, data);
    searchdata_1 = search(lecturerdata_1,data);
    searchdata_2 = search(lecturerdata_2,data);
    searchdata_3 = search(lecturerdata_3,data);
    searchdata_4 = search(lecturerdata_4,data);

    if (searchdata_2.size() > 0)
    {
        for (int a = 0; a < searchdata_2.size(); a++)
        {
            QString temp_1 = "题目(.*?)日期";
            QStringList temp_2 = search(temp_1,searchdata_2[a]);//删除无用信息

            QString temp_3 = "时间(.*?)日期";
            QStringList temp_4 = search(temp_3,searchdata_2[a]);//删除无用信息

            for(int i = 0; i < temp_2.length(); i++)
            {
                searchdata_2[a].replace(temp_2[i], "");
            }
            for(int i = 0; i < temp_4.length(); i++)
            {
                searchdata_2[a].replace(temp_4[i], "");
            }

             searchdata_2[a].replace("报告人：", "");
             searchdata_2[a].replace("日期", "");

             lecturer.append(searchdata_2[a]);
        }
    }
    else if (searchdata.size() > 0)
    {
        for (int a = 0; a < searchdata.size(); a++)
        {
            searchdata[a].replace("报告人：", "");
            searchdata[a].replace("报告时间", "");
            searchdata[a].replace("时间", "");

            lecturer.append(searchdata[a]);
        }
    }
    else if(searchdata_1.size()>0)
    {
        for (int a = 0; a < searchdata_1.size(); a++)
        {
            searchdata_1[a].replace("报告人:", "");
            searchdata_1[a].replace("报告时间", "");

            lecturer.append(searchdata_1[a]);
        }
    }
    else if (searchdata_4.size() > 0)
    {
        for (int a = 0; a < searchdata_4.size(); a++)
        {
            searchdata_4[a].replace("主讲人：", "");
            searchdata_4[a].replace("时间", "");

            lecturer.append(searchdata_4[a]);
        }
    }
    else
    {
        qDebug() << "查找报告人失败";

        lecturer.append("出错\r");
    }

    //qDebug()<<lecturer;



    //查找时间
    regulardata = "时间(.*?)地点";
    QString specialdata_1 = "报告主题一：";//特殊处理
    QString timedata_2 = "日期：(.*?)腾讯会议";
    QString timedata_3 = "时间：(.*?)腾讯会议";

    searchdata = search(regulardata, data);
    searchdata_1 = search(specialdata_1, data);
    searchdata_2 = search(timedata_2, data);
    searchdata_3 = search(timedata_3, data);

    if (searchdata.size() > 0)
    {
        for (int a = 0; a < searchdata.size(); a++)
        {
            searchdata[a].replace("时间：", "");
            searchdata[a].replace("报告地点", "");
            searchdata[a].replace("地点", "");
            searchdata[a].replace("北京时间", "2020年");//特殊处理

            searchdata[a] = date_format(searchdata[a]);//统一时间格式
            time.append(searchdata[a]);
        }
    }
    else if (searchdata_1.size()>0)
    {
        QString timedata_1 = "时间：(.*?)报告摘要";
        QStringList searchdata_3 = search(timedata_1,data);
        for (int a = 0; a < searchdata_3.size(); a++)
        {
            searchdata_3[a].replace("时间：", "");
            searchdata_3[a].replace("报告摘要", "");

            QString time_temp="2019年9月21日"+searchdata_3[a];
            time_temp = date_format(time_temp);//统一时间格式
            time.append(time_temp);
            place.append("华南农业大学数学与信息学院201报告厅");//特殊处理
        }
    }
    else if (searchdata_2.size() > 0)
    {
        for (int a = 0; a < searchdata_2.size(); a++)
        {
            searchdata_2[a].replace("日期：", "");
            searchdata_2[a].replace("时间：", "");
            searchdata_2[a].replace("腾讯会议", "");

            searchdata_2[a] = date_format(searchdata_2[a]);//统一时间格式
            time.append(searchdata_2[a]);
        }
    }
    else if (searchdata_3.size() > 0)
    {
        for (int a = 0; a < searchdata_3.size(); a++)
        {
            searchdata_3[a].replace("日期：", "");
            searchdata_3[a].replace("时间：", "");
            searchdata_3[a].replace("腾讯会议", "");

            searchdata_3[a] = date_format(searchdata_3[a]);//统一时间格式
            time.append(searchdata_3[a]);
        }
    }
    else
    {
        //qDebug()<<"查找时间失败";

        time.append("出错\r");
    }

    //qDebug()<<time;



    //查找地点
    regulardata = "地点(.*?)联系人";

    searchdata = search(regulardata, data);
    searchdata_1 = search(specialdata_1,data);//检查是否是特殊情况

    //对特殊情况进行处理
    if(searchdata_1.size()==0)
    {
        if (searchdata.size() > 0&&searchdata[0].length()<120)
        {
            for (int a = 0; a < searchdata.size(); a++)
            {
                searchdata[a].replace("地点：", "");
                searchdata[a].replace("联系人", "");

                place.append(searchdata[a]);
            }
        }
        else
        {
            regulardata = "地点(.*?)摘要";//特殊处理
            searchdata = search(regulardata, data);
            if (searchdata.size() > 0&&searchdata[0].length()<120)
            {
                for (int a = 0; a < searchdata.size(); a++)
                {
                    QString temp_1 = "主办(.*)";
                    QStringList temp_2 = search(temp_1,searchdata[a]);//删除无用信息

                    for(int i = 0; i < temp_2.length(); i++)
                    {
                        searchdata[a].replace(temp_2[i], "");
                    }

                    searchdata[a].replace("地点：", "");
                    searchdata[a].replace("摘要", "");

                    place.append(searchdata[a]);
                }
            }
            else
            {
                regulardata = "地点(.*?)报告人";
                searchdata = search(regulardata, data);
                if (searchdata.size() > 0)
                {
                    for (int a = 0; a < searchdata.size(); a++)
                    {
                        searchdata[a].replace("地点：", "");
                        searchdata[a].replace("报告人", "");

                        place.append(searchdata[a]);
                    }
                }
                else
                {
                    regulardata = "腾讯会议(.*?)摘要";
                    searchdata = search(regulardata, data);
                    if (searchdata.size() > 0)
                    {

                        for (int a = 0; a < searchdata.size(); a++)
                        {
                            QString temp_1 = "主办(.*)";
                            QStringList temp_2 = search(temp_1,searchdata[a]);//删除无用信息

                            QString temp_3 = "报告(.*)";
                            QStringList temp_4 = search(temp_3,searchdata[a]);//删除无用信息

                            for(int i = 0; i < temp_2.length(); i++)
                            {
                                searchdata[a].replace(temp_2[i], "");
                            }
                            for(int i = 0; i < temp_4.length(); i++)
                            {
                                searchdata[a].replace(temp_4[i], "");
                            }

                            searchdata[a].replace("摘要", "");

                            place.append(searchdata[a]);
                        }
                    }
                    else
                    {
                        qDebug() << "查找地址失败";

                        place.append("出错\r");
                    }
                }
            }
        }
    }

    //qDebug()<<place;



    QStringList senddata;

    for (int a = 0; a < title.length(); a++)
    {
        senddata.append(title[a]);

        senddata.append(lecturer[a < lecturer.size() ? a : lecturer.size() - 1]);

        senddata.append(time[a < time.size() ? a : time.size() - 1]);

        senddata.append(place[a < place.size() ? a : place.size() - 1]);

    }


    //qDebug()<<senddata;


    return senddata;

}

bool scau::dealing(){

    //找出最后更新的讲座

    //获取当前年份
    current_date =QDate::currentDate();


    QSqlQuery query(db->getDataBase());

    query.exec("SELECT link FROM information_list where college='华南农业大学'");

    query.next();//这里指向的是第一条信息
    last_lecture=query.value(0).toString();



    starturl="https://info.scau.edu.cn";

    weburl="/3762/list.htm";

    indexpage=get(starturl+weburl);

    nexturl=search_next_page(indexpage);

    finalurl=search_final_page(indexpage);

    //循环所有页面
    do{
        //获取讲座网页的url
        lecture_list = search_url(indexpage);

        nexturl= search_next_page(indexpage);




        //读取每个网页的信息
        for(int a=0;a<lecture_list.length();a++)
        {
            //CHG
            QString postdate = search_date(get(starturl+lecture_list[a]))[0];
            lecture_date = QDate::fromString(postdate, "yyyy-MM-dd");



            qDebug()<<lecture_list[a]<<lecture_date;


           qDebug()<<"MY_DEBUG :"<<starturl<<" and "<<lecture_list[a];

            //如果与之前更新的讲座重复，跳出
            if(last_lecture.contains(lecture_list[a]))
            {
                qDebug()<<"华南农业大学更新完成";
                nexturl=finalurl;
                break;
            }

            //如果时间超过一年,则跳出
            if(lecture_date.daysTo(current_date)>365)
            {
                qDebug()<<"超过一年";
                nexturl=finalurl;
                break;
            }

            lecture_data = search_data(get(starturl+lecture_list[a]));

            //将信息存入txt
            for(int i=0;i<lecture_data.length();i=i+4)
            {
                QString title,lecture,time,place,url;
                title=lecture_data[i];
                lecture=lecture_data[i+1];
                time=lecture_data[i+2];
                place=lecture_data[i+3];
                url=starturl+lecture_list[a];
                //db->insert_notice(lecture_date_list[a],url);
                db->insert_notice(postdate,url);
                db->insert_report(title,lecture,time,place,"华南农业大学",url);
                qDebug()<<"MyDebug11"<<title<<endl<<lecture<<endl<<time<<endl<<place<<endl<<url<<endl;
                //qDebug()<<endl<<"mydebug22"<<lecture_date_list[a];
            }

        }



        indexpage=get(starturl+nexturl);


    }while(nexturl!=finalurl);



    return true;
}

